using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using GrpcClient.Managers;

namespace GrpcClient.UI
{
    /// <summary>
    /// BrowserTabControl.xaml 的交互逻辑
    /// </summary>
    public partial class BrowserTabControl : UserControl
    {
        public BrowserTabControl()
        {
            InitializeComponent();
            TabManager.Instance.UpdateInputUrlEvent += OnUpdateInput;
        }

        private void OnUpdateInput(string obj)
        {
            input.Text = obj;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var border = sender as Border;
            var tab = border?.DataContext as WebViewControl;
            if (tab != null)
            {
                if (e.MiddleButton == MouseButtonState.Pressed)
                {
                    tab.CloseCommand.Execute(null);
                }
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            var txb = sender as TextBox;
            if (txb != null)
            {
                if (e.Key == Key.Enter)
                {
                    txb.Text = TabManager.FormatUrl(txb.Text);
                    TabManager.Instance.Create(txb.Text);
                }
            }
        }
    }
}
